﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;


namespace DomainInfoLib
{

    public class DomainInfoCacheTxt
    {
        

        
        static List<string> _List = new List<string>();

        static List<string> _NewList = new List<string>();

        static DomainInfoCacheTxt()
        {
            
            string strLine;
       
            FileStream aFile = new FileStream(GetCachePath(), FileMode.Open);

            StreamReader sr = new StreamReader(aFile);

            strLine = sr.ReadLine();

            _List.Add(strLine);
            //Read data in line by line 
            while (strLine != null)
            {
                strLine = sr.ReadLine();

                _List.Add(strLine);
            }

            sr.Close();

                
        }

        

        private static string GetCachePath()
        {
            return System.Configuration.ConfigurationManager.AppSettings["CachePath"];
        } 

        internal static bool Contains(string DomainName)
        {
            try
            {
                if (_List.Contains(DomainName+",true") || _List.Contains(DomainName+",false"))
                {
                    return true;
                }
                return false;
            }
            catch(Exception ex)
            {
                throw ex;
            }

        }





        public static RegisterInfo GetCache(string domainName)
        {
            try
            {
                int trueIndex = _List.IndexOf(domainName + ",true");
                if (trueIndex!=-1)
                {
                    return new RegisterInfo() { Avaliable = true, DomainName = domainName, Response = "", QueryDate = DateTime.Today };
                }
                return new RegisterInfo() { Avaliable = false, DomainName = domainName, Response = "", QueryDate = DateTime.Today };
            }
            catch
            {
                return null;
            }
        }

       

        public static void AddCache(RegisterInfo info)
        {
            try
            {
                if (info==null || info.Avaliable.HasValue==false)
                {
                    return;
                }

                _List.Add(info.DomainName + "," + info.Avaliable.Value.ToString().ToLower());
                _NewList.Add(info.DomainName + "," + info.Avaliable.Value.ToString().ToLower());


                if (_NewList.Count > 1000)
                {
                    Save();

                    _NewList = new List<string>();
                }

            }
            catch
            {

            }
        }



        /// <summary>
        /// 保存缓存
        /// </summary>
        public static void Save()
        {
            SaveTxtFile(GetCachePath());
        }

        public static void SaveTxtFile(string path)
        {
            StreamWriter streamw = File.AppendText(path);
            try
            {
                for (int i = 0; i < _NewList.Count; i++)
                {
                    string text = _NewList[i];

                        streamw.WriteLine(text);

                }

                streamw.Close();

                
            }
            catch (IOException ex)
            {

            }
        }
    }
}
